﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
#if UNITY_EDITOR
using UnityEditor;
#endif
using TileMapEditor;

public class MapInfo : MonoBehaviour
{
    [Header("地图ID")]
    public int mapId;
    [Header("类型：1 秘境 2 迷宫")]
    public int type;
    [Header("初始格ID")]
    [ReadOnly]
    public int startId;
    [Header("地图名称ID")]
    public int mapNameId;
    [Header("地图描述ID")]
    public int mapInfoId;
    [Header("解锁前置关卡")]
    public int unlockBarrierId;
    [Header("小进度宝箱数量")]
    public int boxCount;
    [Header("大进度宝箱数量")]
    public int bigBoxCount;
    [Header("前置地图ID")]
    public int preMapId;
    [Header("是否有迷雾")]
    public bool haveFog;
    [Header("是否有特殊视野规则")]
    public bool haveSpecialSightRule;
    [Header("击杀特定敌人解除迷雾")]
    public int unFogObjId;
    [Header("马车初始朝向(顺时针1-6)")]
    public int startDir;
    [Header("是否在冰面上移动")]
    public bool moveOnIce;
    [Header("触发计数器是否不视为陷阱（不停止）")]
    public bool passByEventCount;

    public void LoadFormMapData(GameMapData gmd)
    {
        mapNameId = gmd.mapNameId;
        mapInfoId = gmd.mapInfoId;
        unlockBarrierId = gmd.unlockBarrierId;
        boxCount = gmd.boxCount;
        bigBoxCount = gmd.bigBoxCount;
        preMapId = gmd.preMapId;
        haveFog = gmd.haveFog;
        startDir = gmd.startDir;
        haveSpecialSightRule = gmd.haveSpecialSightRule;
        unFogObjId = gmd.unFogObjId;
        moveOnIce = gmd.moveOnIce;
        passByEventCount = gmd.passByEventCount;
    }
}

#if UNITY_EDITOR

[CustomEditor(typeof(MapInfo))]
public class MapInfoEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        var mapInfo = (MapInfo)target;

        if (GUILayout.Button("计算进度宝箱数量"))
        {
            LoadBoxCount();
        }
        if(mapInfo.haveFog)
        {
            if (GUILayout.Button("生成迷雾层"))
            {
                CreateFogTilemap();
            }
        }
        else
        {
        }
    }

    private void LoadBoxCount()
    {
        if (!Selection.activeObject) return;


        GameObject mapRoot = (GameObject)Selection.activeObject;
        Transform objRoot = mapRoot.transform.Find("Obj");
        MapInfo mapInfo = (MapInfo)target;
        mapInfo.boxCount = 0;
        mapInfo.bigBoxCount = 0;
        for (int i = 0;i<objRoot.childCount;i++)
        {
            var objSlot = objRoot.GetChild(i);
            for (int j = 0; j < objSlot.childCount; j++)
            {
                var obj = objSlot.GetChild(j).GetComponent<GameObjData>();
                if(obj != null && obj.uiShowType == (int)ObjUIType.UI_RewardBox)
                {
                    int boxType = obj.uiParams[1];//1 小 2 大
                    if(boxType == 1)
                    {
                        //Debug.Log("Small Count ");
                        mapInfo.boxCount++;
                    }
                    if (boxType == 2)
                    {
                        //Debug.Log("Big Count ");
                        mapInfo.bigBoxCount++;
                    }
                }

            }
        }
    }

    private void CreateFogTilemap()
    {
        if (!Selection.activeObject) return;

        GameObject mapRoot = (GameObject)Selection.activeObject;
        Transform root = mapRoot.transform.Find("Walkable");
        GameObject fog = Instantiate(root.gameObject, mapRoot.transform);
        fog.name = "Fog";
        var tileMap = fog.GetComponent<Tilemap>();
        tileMap.color = new Color(0.5f, 0.5f, 0.5f, 1);
        var tileRenderer = fog.GetComponent<TilemapRenderer>();
        tileRenderer.sortingLayerName = "UI";
        tileRenderer.sortingOrder = 5;
        tileRenderer.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;

        Transform blockRoot = mapRoot.transform.Find("Block");
        GameObject blockFog = Instantiate(blockRoot.gameObject, mapRoot.transform);
        blockFog.name = "BlockFog";
        tileMap = blockFog.GetComponent<Tilemap>();
        tileMap.color = new Color(0.5f, 0.5f, 0.5f, 1);
        tileRenderer = blockFog.GetComponent<TilemapRenderer>();
        tileRenderer.sortingLayerName = "UI";
        tileRenderer.sortingOrder = 6;
        tileRenderer.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;

        GameObject fogSlotPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Dungeon/Res/TileMap/Prefab/Tile/FogSlot.prefab");
        GameObject fogSlot = Instantiate<GameObject>(fogSlotPrefab, fog.transform);
        fogSlot.name = "FogSlot";
        fogSlot.transform.localEulerAngles = Vector3.zero;
        var spriteMask = fogSlot.GetComponent<SpriteMask>();
        spriteMask.backSortingOrder = 4;
        spriteMask.frontSortingOrder = 7;
        var script = fogSlot.GetComponent<SpritePatternModifier>();
        script.setToCenter = true;

        EditorUtility.SetDirty(mapRoot);
    }
}

#endif
